
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>ecma6 学习总结 - niko</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="我想分享给你一些我的想法,变量声明--let
---
let特性
1. 不可以重复申明
2. 没有预解析过程（即变量声明提前）
3. 块级作用域 ：一个变量或者函数只有在该区域才起作用
    ````javascript
,"> 
    <meta name="author" content="niko"> 
    <link rel="alternative" href="atom.xml" title="niko" type="application/atom+xml"> 
    <link rel="icon" href="/img/logo_icon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">niko</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/logo_icon.png" alt="" data-url="http://yoursite.com">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">ecma6 学习总结</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/EcmaJs6"><b>「
                    </b>ECMAJS6<b> 」</b></a>
                
                十二月 06, 2016
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2016/12/06/%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/" title="ecma6 学习总结" class="">ecma6 学习总结</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>文章字数</i>
                    3.6k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>阅读约需</i>
                    3 mins.
                </span>
                
                
                <span id="/2016/12/06/学习总结/" class="leancloud_visitors" data-flag-title="ecma6 学习总结">
                    <b class="iconfont icon-read"></b> <i>阅读次数</i>
                    <span class="leancloud-visitors-count">1000000</span>
                </span>
                
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/es6/" rel="tag">es6</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <h6 style="color:green">变量声明--let</span>
---
<b>let特性</b>
1. 不可以重复申明
2. 没有预解析过程（即变量声明提前）
3. 块级作用域 ：一个变量或者函数只有在该区域才起作用
    ````javascript
     { //块级作用域
        let a = 1;
        console.log(a)
     }

<pre><code> //举个例子：for循环依次输出 0~10
 for(var i = 0 ;i&lt;10;i++){
  setTimeout(function(){
      console.log(i);//解析：上述结果 输出10次i ，原因为每次for循环结束后 setTimeout还在走，所以输出的i还是全局的i，所以一直是10
  },0)
 }
 //我们若想依次输出0~10 我们可以使用一个闭包函数， 在闭包函数中会形成一个作用域，将i以形参的形式传入，这样每次执行循环的时候，变量就会存在于闭包作用域中。
 for(var i = 0 ;i&lt;10;i++){
    (function(i){
     setTimeout(function (argument) {
         console.log(i);  // 0,1,2,3,4,5,6,7,8,9,10
     })
    })(i)
}
//在ecma6 中 我们可以使用let 来实现 ,let 将循环变成一个块级作用域，第一次循环完成之后，i就是那个0
for(let i = 0;i&lt;10;i++){
    setTimeout(function (argument) {
        console.log(i);  // 0,1,2,3,4,5,6,7,8,9,10
    })
}
````

&lt;!-- more --&gt;</code></pre><ol start="4">
<li>暂存死区   相似于 预解析  </li>
</ol>
<h6 style="color:green">常量声明--const</span>
---
<b >const简介:</b>防止命名冲突  存一些东西用于对比  声明后不能被修改(变量值不可修改，若是一个对象是可以被修改的)

<pre><code class="javascript">    const a = 1 ;
    console.log(a) //1
    // const a = 2 ;
    console.log(a) //报错

    const b = {name:1};
    console.log(a.name);
    ab.name = 2 ;
    console.log(a) // {name:2}</code></pre>
<h6 style="color:green">变量的<b>"解构"</b>赋值</span>
---
<b >解构赋值简介:</b> Es6 允许按照一定的模式，通过一定的格式将数组和对象中的值提取出来，赋给变量
```javascript
"use strict"
//数组的解构赋值 ： 按照对应的顺序解构
var arr = [1,2,3] ;
var [a,b,c] = arr ;
console.log(a,b,c)  // 1,2,3
var arr1  = [1,[2,3]] ;
var [d,[e,f]] =  arr ;
console.log(d,e,f)  // 1,2,3
//对象的解构赋值 ： 按照 对象的属性名称进行解构   和顺序无关 ,方便从对象里面拿到对应的值
var obj = {
    foo:function(){},
    o:{},
    arr:[],
    str:'abc'
}
var {foo,o,arr,str} = obj ;
console.log(foo,o,arr,str) ; // function(){}  {}  []  abc    
//值的交换
var x = 1 ;
var y = 2 ;
var [x,y] = [y,x];
console.log(x,y) ; //2 1
````

<h6 style="color:green">ES6字符串的扩展</span>
---
```javascript
/*模板字符串*/
 var name = '狮父' ;
 var age = '18' ;
//传统字符串拼接做法：
 var str = "你的名字是："+name+"你的年龄是"+age ;
 console.log(str)  
//使用模板字符串
 varstr = `你的名字是${name},你的年龄是${age}`  //注意：使用反引号 `do something` 来包含,{}中可以进行运算 和 对象.属性
/*字符串复制str.repeat(num);*/
 var str = a ;
 console.log(a.repeat(10)) // aaaaaaaaaa
/*字符串的查找 str.includes()*/
 var str = 'abc' ;
 console.log(str.includes('a'))  //true
/*查找对应字符是否在首部或者尾部*/
 var str = "shifu" ;
 str.startsWith('shi') // true ;
 str.endsWith('u') ; //true ;
```
<h6 style="color:green">ES6数值的扩展</span>
---
```javascript
/*二进制和八进制表示法:ES6提供使用前缀Ob和Oo表示 二进制八进制*/
var a = 0b11 ; // 3
var a = 0o11 ; // 9
/*Math 方法的扩展*/
// 取掉小数点
var num = 1.234;
console.log(Math.trunc(num)) // 1
//判断一个数字是正数负数,正0还是0
var num1 = 0 ;
console.log(Math.sign(0)) //0 ;
console.log(Math.sign(-0)) //  -0 ;
//勾股定理  返回所有平方和的平方根
console.log(Math.hypot(3,4))  //5    
```
<h6 style="color:green">ES6数组的扩展</span>
---
```javascript
/*转数组*/
"use strict"
var divs = document.getElementsByTagName('div')   //获取到类数组
//es5做法
var ele = [].slice.call('div') ; // []
//es6 做法
var eles =  Array.from(divs) ;   //[]
//也可以字符串转数组
var str = 'abc' ;
console.log(Array.from(str))   // ['a','b','c']
//将参数转换成数组   es5中:var arr = new Array(5) ,arr.length == 5 ;
var arr  = Array.of(a,b,c,d,e) ;
console.log(arr)  // [] ;
/*数组查找元素*/
//语法:arr.find()   arr.findIndex()
//参数:1.回掉函数  2.回掉函数中的this指向
//执行过程:遍历整个数组,遍历的过程中调用回调函数，如果回调函数的返回值为true，则返回当前正在遍历的元素，如果所有的元素都不符合条件则返回undefined
var arr = [1,2,3,4,5] ;
var n = arr.find(function(value,index,ar){
    console.log(value) ; // 遍历的当前值  1
    console.log(index) ; // 遍历值所处的位置索引 0
    console.log(ar) ; // 当前正在遍历的数组本身  [1,2,3,4,5]
})


<pre><code>/*new*/</code></pre>
            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d=''
    >留言</div>


            
            
            <div id="vcomments"></div>
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>niko</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/TriDiamond" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="https://twitter.com/TriDiamond6" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="https://stackoverflow.com/users/7602324/tridiamond?tab=profile" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">20 <p>文章</p></a></li>
                    <li><a href="/categories">10 <p>分类</p></a></li>
                    <li><a href="/tags">11 <p>标签</p></a></li>
                </ul>
            </div>
            
            
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            niko
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>


    
<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

    <script>

        var valine = new Valine();

        function initValine(path) {
            if (!path) path = window.location.pathname;
            let language = 'zh-CN';
            if (!language) {
                language = 'en';
            } else {
                language = language.toLowerCase();
            }
            valine.init({
                el: '#vcomments',
                appId: 'h0Nbcp32WtIpnnHaGTLWbtKo-gzGzoHsz',
                appKey: 'hPhFiAi2vIj2OStCIi79waty',
                notify: 'false',
                verify: 'false',
                avatar: 'happ_niko',
                placeholder: '快写下你的想法与我互动',
                visitor: 'true',
                path: path,
                lang: language
            });
        }

        $(document).ready(function () {
            initValine();
        });
    </script>



</html>
